<?php

namespace app\geekopen\controller;

use support\Request;
use Workerman\Protocols\Http\Chunk;

class SwitchController
{
    //获取插座状态信息
    public function index(Request $request)
    {
        $http_connection = $request->connection;
        $device_name = $request->post('device_name','');
        if(!$device_name){
            return json(['code'=>0,'msg'=>'缺失参数device_name']);
        }
        $appId = $request->post('appId','');
        if(!$appId){
            return json(['code'=>0,'msg'=>'缺失参数appId']);
        }
        $deviceKey = $request->post('deviceKey','');
        if(!$deviceKey){
            return json(['code'=>0,'msg'=>'缺失参数deviceKey']);
        }
        $username = $request->post('username','');
        if(!$username){
            return json(['code'=>0,'msg'=>'缺失参数username']);
        }
        $password = $request->post('password','');
        if(!$password){
            return json(['code'=>0,'msg'=>'缺失参数password']);
        }
        $client_id = $request->post('client_id','');
        if(!$client_id){
            return json(['code'=>0,'msg'=>'缺失参数client_id']);
        }
        $mqtt = new \Workerman\Mqtt\Client('mqtt://mqtt.geek-smart.cn:1883', array(
            'debug' => true,
            "username"=>$username, "password"=>$password,"client_id"=>$client_id
        ));
        $mqtt->onConnect = function($mqtt) use ($device_name,$appId,$deviceKey) {
            $mqtt->subscribe('/'.$appId.'/'.$deviceKey.'/'.$device_name.'/subscribe');
            $data['type'] = 'info';
            $str = json_encode($data,320);
            $mqtt->publish('/'.$appId.'/'.$deviceKey.'/'.$device_name.'/publish', $str);//向主题发送内容

        };
        $mqtt->onMessage = function($topic, $content) use($http_connection,$mqtt){
            $json = json_decode($content,true);
            $http_connection->send(new Chunk(json_encode($json,320)));
            $http_connection->send(new Chunk('')); // 发送空的的chunk代表response结束
            $mqtt->close();
            
            
        };
        $mqtt->onError = function(\Exception $exception=null) use($mqtt,$http_connection){
            $err = ['code'=>0,'msg'=>'连接出错'];
            $json = json_encode($err,320);
            $http_connection->send(new Chunk($json));
            $http_connection->send(new Chunk('')); // 发送空的的chunk代表response结束
            $mqtt->close();
        };
        $mqtt->connect();
        return response()->withHeaders([
            "Transfer-Encoding" => "chunked",
            "Content-Type"=>"application/json"
        ]);
    }
    //获取插座电量信息
    public function statistic(Request $request)
    {
        $http_connection = $request->connection;
        $device_name = $request->post('device_name','');
        if(!$device_name){
            return json(['code'=>0,'msg'=>'缺失参数device_name']);
        }
        $appId = $request->post('appId','');
        if(!$appId){
            return json(['code'=>0,'msg'=>'缺失参数appId']);
        }
        $deviceKey = $request->post('deviceKey','');
        if(!$deviceKey){
            return json(['code'=>0,'msg'=>'缺失参数deviceKey']);
        }
        $username = $request->post('username','');
        if(!$username){
            return json(['code'=>0,'msg'=>'缺失参数username']);
        }
        $password = $request->post('password','');
        if(!$password){
            return json(['code'=>0,'msg'=>'缺失参数password']);
        }
        $client_id = $request->post('client_id','');
        if(!$client_id){
            return json(['code'=>0,'msg'=>'缺失参数client_id']);
        }
        $mqtt = new \Workerman\Mqtt\Client('mqtt://mqtt.geek-smart.cn:1883', array(
            'debug' => true,
            "username"=>$username, "password"=>$password,"client_id"=>$client_id
        ));
        $mqtt->onConnect = function($mqtt) use ($device_name,$appId,$deviceKey) {
            $mqtt->subscribe('/'.$appId.'/'.$deviceKey.'/'.$device_name.'/subscribe');
            $data['type'] = 'statistic';
            $str = json_encode($data,320);
            $mqtt->publish('/'.$appId.'/'.$deviceKey.'/'.$device_name.'/publish', $str);//向主题发送内容

        };
        $mqtt->onMessage = function($topic, $content) use($http_connection,$mqtt){
            $json = json_decode($content,true);
            $http_connection->send(new Chunk(json_encode($json,320)));
            $http_connection->send(new Chunk('')); // 发送空的的chunk代表response结束
            $mqtt->close();
            
            
        };
        $mqtt->onError = function(\Exception $exception=null) use($mqtt,$http_connection){
            $err = ['code'=>0,'msg'=>'连接出错'];
            $json = json_encode($err,320);
            $http_connection->send(new Chunk($json));
            $http_connection->send(new Chunk('')); // 发送空的的chunk代表response结束
            $mqtt->close();
        };
        $mqtt->connect();
        return response()->withHeaders([
            "Transfer-Encoding" => "chunked",
            "Content-Type"=>"application/json"
        ]);
    }
     //控制插座
    public function eventkey(Request $request)
    {
        $http_connection = $request->connection;
        $device_name = $request->post('device_name','');
        if(!$device_name){
            return json(['code'=>0,'msg'=>'缺失参数device_name']);
        }
        $appId = $request->post('appId','');
        if(!$appId){
            return json(['code'=>0,'msg'=>'缺失参数appId']);
        }
        $deviceKey = $request->post('deviceKey','');
        if(!$deviceKey){
            return json(['code'=>0,'msg'=>'缺失参数deviceKey']);
        }
        $username = $request->post('username','');
        if(!$username){
            return json(['code'=>0,'msg'=>'缺失参数username']);
        }
        $password = $request->post('password','');
        if(!$password){
            return json(['code'=>0,'msg'=>'缺失参数password']);
        }
        $client_id = $request->post('client_id','');
        if(!$client_id){
            return json(['code'=>0,'msg'=>'缺失参数client_id']);
        }
        $key = $request->post('key',0);
        $mqtt = new \Workerman\Mqtt\Client('mqtt://mqtt.geek-smart.cn:1883', array(
            'debug' => true,
            "username"=>$username, "password"=>$password,"client_id"=>$client_id
        ));
        $mqtt->onConnect = function($mqtt) use ($device_name,$appId,$deviceKey,$key) {
            $mqtt->subscribe('/'.$appId.'/'.$deviceKey.'/'.$device_name.'/subscribe');
            $data['type'] = 'event';
            $data['key'] = $key;
            $str = json_encode($data,320);
            $mqtt->publish('/'.$appId.'/'.$deviceKey.'/'.$device_name.'/publish', $str);//向主题发送内容

        };
        $mqtt->onMessage = function($topic, $content) use($http_connection,$mqtt){
            $json = json_decode($content,true);
            $http_connection->send(new Chunk(json_encode($json,320)));
            $http_connection->send(new Chunk('')); // 发送空的的chunk代表response结束
            $mqtt->close();
            
            
        };
        $mqtt->onError = function(\Exception $exception=null) use($mqtt,$http_connection){
            $err = ['code'=>0,'msg'=>'连接出错'];
            $json = json_encode($err,320);
            $http_connection->send(new Chunk($json));
            $http_connection->send(new Chunk('')); // 发送空的的chunk代表response结束
            $mqtt->close();
        };
        $mqtt->connect();
        return response()->withHeaders([
            "Transfer-Encoding" => "chunked",
            "Content-Type"=>"application/json"
        ]);
    }
}
